package com.example.jobdemo.controller;

import com.example.jobdemo.entity.Company;
import com.example.jobdemo.entity.UserCollect;
import com.example.jobdemo.mapper.CompanyMapper;
import com.example.jobdemo.mapper.UserCollectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("company")
public class CompanyController {

    @Autowired
    CompanyMapper companyMapper;

    @Autowired
    UserCollectMapper userCollectMapper;

    @RequestMapping("page")
    public Map<String, Object> page(@RequestBody Company company) {
        Map<String, Object> map = new HashMap<>();
        Integer count = companyMapper.selectCompanyCount(company.getTitle(),company.getPostCity());
        int pageIndex = (company.getPageIndex() -1)* company.getPageSize();
        List<Company> companyList = companyMapper.selectCompanyList(company.getTitle(),company.getPostCity(),pageIndex,company.getPageSize());
        map.put("code", 200);
        map.put("msg", "成功");
        map.put("data", companyList);
        map.put("count",count);
        return map;
    }

    @RequestMapping("details")
    public Map<String, Object> details(@RequestBody Company company) {
        Company data = companyMapper.selectByPrimaryKey(company.getCompanyId());
        UserCollect userCollect = userCollectMapper.selectByCompanyIdAndUserId(company.getUserId(),company.getCompanyId());
        if (userCollect!=null) {
            company.setUserCollectId(userCollect.getUserCollectId());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "成功");
        map.put("data", data);
        return map;
    }
}
